package com.lsmgc.cms.util;

import com.lsmgc.cms.util.dto.ParamDto;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class RequestUtils {

    /**
     * 从request流中解析参数与上传的文件
     *
     * @param request
     */
    public static ParamDto parseParam(HttpServletRequest request) {
        ParamDto result = new ParamDto();
        ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
        upload.setHeaderEncoding("UTF-8");
        try {
            List<FileItem> fileItemList = upload.parseRequest(request);
            for (FileItem fileItem : fileItemList) {
                if (fileItem.isFormField()) {
                    result.getParamMap().put(fileItem.getFieldName(), fileItem.getString("UTF-8"));
                } else {
                    result.getFileMap().put(fileItem.getFieldName(), fileItem);
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static int getInt(HttpServletRequest request, String str) {
        String tempStr = request.getParameter(str);
        int tempInt = 0;
        if (tempStr == null || "".equals(tempStr)) {
            tempStr = "0";
        }
        try {
            tempInt = Integer.parseInt(tempStr);
        } catch (Exception e) {
            e.printStackTrace();
            tempInt = 0;
            e.printStackTrace();
        }
        return tempInt;
    }

    public static int getInt(HttpServletRequest request, String str, int defaultInt) {
        String tempStr = request.getParameter(str);
        int tempInt = 0;
        if (tempStr == null || "".equals(tempStr)) {
            tempInt = defaultInt;
            return tempInt;
        }
        try {
            tempInt = Integer.parseInt(tempStr);
        } catch (Exception e) {
            e.printStackTrace();
            tempInt = 0;
            e.printStackTrace();
        }
        return tempInt;
    }

    public static Long getLong(HttpServletRequest request, String str) {
        String tempStr = request.getParameter(str);
        Long tempInt = 0L;
        if (tempStr == null || "".equals(tempStr)) {
            tempStr = "0";
        }
        try {
            tempInt = Long.valueOf(tempStr);
        } catch (Exception e) {
            e.printStackTrace();
            tempInt = 0L;
            e.printStackTrace();
        }
        return tempInt;
    }

    public static Long getLong(HttpServletRequest request, String str, Long defaultInt) {
        String tempStr = request.getParameter(str);
        Long tempInt = 0L;
        if (tempStr == null || "".equals(tempStr)) {
            tempInt = defaultInt;
            return tempInt;
        }
        try {
            tempInt = Long.valueOf(tempStr);
        } catch (Exception e) {
            e.printStackTrace();
            tempInt = 0L;
            e.printStackTrace();
        }
        return tempInt;
    }
}
